import {cursorGetData, deleteDB, getDataByIndex, getDataByKey, openDB, updateDB} from "../util/indexDBUtil.js";
let storeName='accounts'
let db

export const accounts=[]

export const accountManage={
    open:async () => {
        db = await openDB(storeName,1,(event)=>{
            // 数据库创建或升级的时候会触发
            console.log("onupgradeneeded");
            db = event.target.result; // 数据库对象
            var objectStore;
            // 创建存储库
            objectStore = db.createObjectStore(storeName, {
                keyPath: "id", // 这是主键
                // autoIncrement: true // 实现自增
            });
            // 创建索引，在后面查询数据的时候可以根据索引查
            objectStore.createIndex("name", "name", { unique: false });
        })
    },
    getAll : async () => {

        let ls = await cursorGetData(db, storeName)
        return ls
    },
    getById : (id) => {
        return  getDataByKey(db, storeName,id)
    },
    saveOrUpdate : (jsonData) => {
        updateDB(db,storeName,JSON.parse(jsonData))
    },
    delById : (id) => {
        deleteDB(db,storeName,id)
    }
}

await accountManage.open()



